package com.fjh.springboot_01.service.impl;

import com.fjh.springboot_01.pojo.homework.KnowledgePoint;
import com.fjh.springboot_01.mapper.KnowledgePointMapper;
import com.fjh.springboot_01.service.KnowledgePointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class KnowledgePointServiceImpl implements KnowledgePointService {
    @Autowired
    private KnowledgePointMapper knowledgePointMapper;

    @Override
    public void addKnowledgePoint(KnowledgePoint knowledgePoint) {
        knowledgePointMapper.insert(knowledgePoint);
    }

    @Override
    public List<KnowledgePoint> getKnowledgePoints() {
        return knowledgePointMapper.findAll();
    }

    @Override
    public List<KnowledgePoint> getKnowledgePointsByCategory(Integer categoryId) {
        return knowledgePointMapper.findByCategoryId(categoryId);
    }

    @Override
    public void updateKnowledgePoint(KnowledgePoint knowledgePoint) {
        knowledgePointMapper.update(knowledgePoint);
    }

    @Override
    public void deleteKnowledgePoint(Integer id) {
        knowledgePointMapper.delete(id);
    }

    @Override
    @Transactional
    public void addHomeworkKnowledge(Integer homeworkId, List<Integer> knowledgeIds) {
        knowledgePointMapper.deleteHomeworkKnowledge(homeworkId);
        if (knowledgeIds != null && !knowledgeIds.isEmpty()) {
            for (Integer knowledgeId : knowledgeIds) {
                knowledgePointMapper.addHomeworkKnowledge(homeworkId, knowledgeId);
            }
        }
    }

    @Override
    public List<KnowledgePoint> getHomeworkKnowledge(Integer homeworkId) {
        return knowledgePointMapper.findByHomeworkId(homeworkId);
    }
} 